const Model = require("../models/Model");
let jwt = require("jsonwebtoken");
global.secretKey = "jwt";

module.exports = {
  registery(req, res) {
    Model.userRegistery
      .insertMany(req.body)
      .then((success) => {
        res.send({ data: success, status: 200 });
      })
      .catch((err) => {
        res.send({ data: err, status: 500 });
      });
  },
  async login(req, res) {
    console.log(req.body);
    let loginInfo = req.body;
    let data = await Model.userRegistery.find({
      username: loginInfo.username,
      password: loginInfo.password,
    });
    if (data.length > 0) {
      res.send({
        data: data,
        status: 200,
        token: jwt.sign({ username: loginInfo.username }, global.secretKey, {
          expiresIn: "1h",
        }),
      });
      return;
    }
    res.send({ data: null, status: 500 });
  },
};
